<?php

/**
 * 默认控制器
 */
class Controller_Systemlog extends Controller_Abstract
{
    /* ******************************************************************** */
    function actionOutput()
    {
        if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));
		
		$show_box['title'] = '查询系统日志';
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');
		$this->_view['form'] = new Form_BeSearch(url('default::systemlog/hostlist'));
		$this->_view['form']->_subject = '查询系统日志';
		//查询，导出和批量删除
        //dump($_SESSION);
    }

	function actionOutputs()
    {
        if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));
		
		$show_box['title'] = '查询系统日志';
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');
		$this->_view['form'] = new Form_BeSearch(url('default::systemlog/hostlist',array('id' => $this->_context->id)));
		$this->_view['form']->_subject = '查询系统日志';

		if(isset($this->_context->mac)){
			$this->_view['form']->add(QForm::ELEMENT, 'mac', array('_ui' => 'hidden','value' => $this->_context->mac));
		}

		if(isset($this->_context->id)){
			$this->_view['form']->add(QForm::ELEMENT, 'id', array('_ui' => 'hidden','value' => $this->_context->id));
		}
		//查询，导出和批量删除
        //dump($_SESSION);
    }

	function actionDetails(){
		
		if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));
		
		$show_box['title'] = '系统日志-详细内容';
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');

		$this->_view['sk'] = '';
		$this->_view['theno'] = $this->_context->tableid;
		$iftype=$this->_context->tablename;
		$theno=$this->_context->tableid;
		switch ($iftype)
		{
				case '':
				  $select = OldEventlog::find('id =?',$theno);
				  break;
				default:
				  $select = OldEventlog::find('id =?',$theno);
		}

		//上下条
		$this->_view['prev']='';$this->_view['next']='';
		$table =QDB::getConn();
		$sql = "SELECT id FROM `eventlog` WHERE id < ".$theno." ORDER BY id DESC LIMIT 1";
		$handel =$table->execute($sql);
		$temp2 = $handel->fetchAll();
		foreach ($temp2 as $s){
			if(isset($s['id']))
				$this->_view['prev']=$s['id'];
			else
				$this->_view['prev']='';
		}

		$sql = "SELECT id FROM `eventlog` WHERE id > ".$theno." ORDER BY id ASC LIMIT 1";
		$handel =$table->execute($sql);
		$temp2 = $handel->fetchAll();
		foreach ($temp2 as $s){
			if($s['id'])
				$this->_view['next']=$s['id'];
			else
				$this->_view['next']='';
		}
		
		$this->_view['group'] = $select->order("id desc")->getAll();
	}

    /* ******************************************************************** */
    function actionIndex()
    {
        $show_box['title'] = '系统日志首页';
        if ($this->_app->currentUserRoles())
        {
			Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');

			$this->_view['form'] = new Form_SysFliter(url('default::systemlog/index', array('mac' => $this->_context->mac,'startd' => $this->_context->startd,'endd' => $this->_context->endd)));

			$addconds="time >= '".date("Y-m-d")." 00:00:00' and time <= '".date("Y-m-d")." 23:59:59'";
			$this->_view['startd']='';
			$sdstr='';$edstr='';$tstr='';$cstr='';$macstr='';
			$ostr='id';$osstr='desc';
			$ostrs='&ordercol='.$ostr.'&desc='.$osstr;
			$sostr='';$idstr='';$constr='';
			$this->_view['endd']='';
			$this->_view['macs']='';$perstr='';
			$pagesize = 10;
			$addmac='';$addcontent='';$edit_one=array();
			if ($this->_context->isPOST())
			{
				$edit_one['szsource']=$_POST['szsource'];
				//$edit_one['sztype']=$_POST['sztype'];
				$edit_one['sztype']='as';
				$edit_one['szdesc']=$_POST['szdesc'];
				$edit_one['logid']=$_POST['logid'];
				$edit_one['ordercol']=$_POST['ordercol'];
				$edit_one['desc']=$_POST['desc'];
				$this->_view['form']->import($edit_one);

				if(isset($_POST['perpage']) && $_POST['perpage']!=''){
					$pagesize = $_POST['perpage'];
					$perstr='&per='.$_POST['perpage'];
				}

				if(isset($_POST['enddate']) && isset($_POST['startdate'])){
					if($_POST['startdate']==$_POST['enddate']){
						//$endd=$this->my_date_add('d',$_POST['enddate'],1);
					}else{
						//$endd=$_POST['enddate'];
					}

					$this->_view['startd']=$_POST['startdate'];
					$this->_view['endd']=$_POST['enddate'];
				
				}
				
				if(isset($_POST['mac'])){
					$this->_view['macs']=$_POST['mac'];
					$addmac=" and macaddress='".$_POST['mac']."' ";
					$macstr="&mac=".$_POST['mac'];
				}

				//内容关键字
				if($_POST['szdesc']!=''){
					$this->_view['szdesc']=$_POST['szdesc'];
					//$addcontent.=" and szdesc like '%".$_POST['szdesc']."%' ";
					//如果有空格
					$c=explode(' ',$_POST['szdesc']);
					if(count($c)>1){  
						$alln=array();
						for($i=0; $i < count($c); $i++) {
							if($c[$i]!='')
								$alln[]=" szdesc like '%".str_replace('\\','\\\\',addslashes($c[$i]))."%' ";
						}
						$addcontent.=" and (".join(' and ',$alln).") ";
					}else{
						$addcontent.=" and szdesc like '%".str_replace('\\','\\\\',addslashes($_POST['szdesc']))."%' ";
					}
					$constr='&szdesc='.urlencode($_POST['szdesc']);
				}

				//来源关键字
				if($_POST['szsource']!=''){
					$this->_view['szsource']=$_POST['szsource'];
					$addcontent.=" and szsource like '%".$_POST['szsource']."%' ";
					$sostr='&szsource='.$_POST['szsource'];
				}

				//类型关键字
				if(isset($_POST['sztype'])){
					
					$this->_view['sztype']=$_POST['sztype'];
					if($_POST['sztype']!='all'){
					$addcontent.=" and sztype = '".$_POST['sztype']."' ";
					$tstr='&infotype='.$_POST['sztype'].'';
					
					}
				}

				//事件ID
				if($_POST['logid']!=''){
					$this->_view['logid']=$_POST['logid'];
					$addcontent.=" and logid = ".$_POST['logid']." ";
					$idstr='&logid='.$_POST['logid'];
				}

				//order排序关键字
				if($_POST['ordercol']!='' && $_POST['desc']!=''){
					$this->_view['ordercol']=$_POST['ordercol'];
					$this->_view['desc']=$_POST['desc'];
					//$addcontent.=" and szdesc like '%".$_POST['szdesc']."%' ";
					$ostr=$_POST['ordercol'];
					$osstr=$_POST['desc'];
					$ostrs='&ordercol='.$ostr.'&desc='.$osstr;
				}else{
					$this->_view['ordercol']='id';
					$this->_view['desc']=$_POST['desc'];
					$ostr='id';
					$osstr=$_POST['desc'];
					$ostrs='&ordercol='.$ostr.'&desc='.$osstr;
				}

				if(isset($_POST['enddate']) && isset($_POST['startdate'])){
					$addconds="time >='".$_POST['startdate']." 00:00:00' and time<='".$_POST['enddate']." 23:59:59'".$addmac.$addcontent;
				
					if($_POST['startdate']!='')
						$sdstr='&startd='.$_POST['startdate'].'';

					if($_POST['enddate']!='')
						$edstr='&endd='.$_POST['enddate'].'';
				}else{
					$addconds=$addmac.$addcontent;
				}
				//echo $this->my_date_add('d',$_POST['enddate'],1);
				//echo $addconds;
				//dump($_POST);
			}else{
				$this->_view['form']->element('sztype')->set('selected', 'all');
			}

			if($this->_context->startd !='' && $this->_context->endd !='' && !$this->_context->isPOST()){
				//echo $this->_context->macs;
				
				$this->_view['startd']=$this->_context->startd;
				$this->_view['endd']=$this->_context->endd;
				
				if($this->_context->macs!=''){
					$addmac=" and macaddress='".$this->_context->macs."' ";
				}

				$addconds="time >='".$this->_context->startd." 00:00:00' and time<='".$this->_context->endd." 23:59:59'".$addmac.$addcontent;

				if($this->_context->startd!='')
					$sdstr='&startd='.$this->_context->startd.'';

				if($this->_context->startd!='')
					$edstr='&endd='.$this->_context->endd.'';
			}

			if (!$this->_context->isPOST()){
				
				if(isset($this->_context->infotype)){
					
					if($this->_context->infotype=='all'){
						$tstr='&infotype='.$this->_context->infotype.'';
						$addconds .=" and sztype = '".$this->_context->infotype."' ";
					}
				}

				if($this->_context->logid!=''){
					$idstr='&logid='.$this->_context->logid.'';
					$addconds .= " and logid = ".$this->_context->logid." ";
				}

				if($this->_context->szsource!=''){
					$sostr='&szsource='.$this->_context->szsource.'';
					$addconds .= " and szsource like '%".$this->_context->szsource."%' ";
				}

				if($this->_context->szdesc!=''){
					
					$c=explode(' ',$this->_context->szdesc);
					if(count($c)>1){  
						$alln=array();
						for($i=0; $i < count($c); $i++) {
							if($c[$i]!='')
								$alln[]=" szdesc like '%".str_replace('\\','\\\\',addslashes($c[$i]))."%' ";
						}
						$addconds.=" and (".join(' and ',$alln).") ";
						//dump($addcontent);
					}else{
						$addconds.=" and szdesc like '%".str_replace('\\','\\\\',addslashes($this->_context->szdesc))."%' ";
					}
					
					//$addconds .=" and szdesc like '%".$this->_context->szdesc."%' ";
					//dump($this->_context->szdesc);
					$constr='&szdesc='.urlencode($this->_context->szdesc).'';
				}
				
				//$edit_one['szsource']=$_POST['szsource'];
				$edit_one['szsource']=$this->_context->szsource;
				$edit_one['szdesc']=$this->_context->szdesc;
				$edit_one['logid']=$this->_context->logid;
				$edit_one['sztype']=$this->_context->infotype;
				$edit_one['ordercol']=$this->_context->ordercol;
				$edit_one['desc']=$this->_context->desc;
				//$edit_one['szdesc']=$_POST['szdesc'];
				//$edit_one['logid']=$_POST['logid'];
				$this->_view['form']->import($edit_one);
			}

			if(isset($this->_context->per) && $this->_context->per!=''){
				$pagesize = $this->_context->per;
				$perstr='&per='.$this->_context->per;
			}
			
			if($this->_context->mac==''){
			$addconds=iconv("UTF-8","GB2312",$addconds);
			
			// 获取当前页数，通常为get方式参数
			$page = $this->_context->page;
			$this->_view['pageno']=$page;
			$select = OldEventlog::find($addconds)->limitPage($page, $pagesize);
			$this->_view['pager'] = $select->getPagination();
			$this->_view['group'] = $select->order($ostr." ".$osstr)->getAll();
			
			

			if($this->_view['pager']['record_count']>0 && $page=='')
				$page=1;
				//echo $this->_view['pager']['record_count'];
				$pagediv = new Helper_MyPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'index.php?controller=systemlog'.$sostr.$idstr.$constr.$sdstr.$edstr.$tstr.$cstr.$macstr.$ostrs.$perstr.'&page=','',5);
				$this->_view['pagestr']= $pagediv->show();

			}else{
				
				

				//$pagesize = 10;
				// 获取当前页数，通常为get方式参数
				$page = $this->_context->page;
				$this->_view['pageno']=$page;
				$this->_view['macs']=$this->_context->mac;
				$andstr='';
				if($addconds!='')$andstr=' and ';

				$allconditions=$addconds.$andstr;
				//dump($allconditions);
				$allconditions=iconv("UTF-8","GB2312",$allconditions);
				$select = OldEventlog::find($allconditions.'macaddress = ?', $this->_context->mac)->limitPage($page, $pagesize);
				$this->_view['pager'] = $select->getPagination();
				$this->_view['group'] = $select->order($ostr." ".$osstr)->getAll();
				$macstr="&mac=".$this->_context->mac;
				if($this->_view['pager']['record_count']>0 && $page=='')
				$page=1;
				//echo $this->_view['pager']['record_count'];
				$pagediv = new Helper_MyPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'index.php?controller=systemlog'.$sostr.$idstr.$constr.$sdstr.$edstr.$tstr.$cstr.$macstr.$ostrs.$perstr.'&page=','',5);
				$this->_view['nowperpage']=$pagesize;
				$this->_view['pagestr']= $pagediv->show();

			}
        }
        else
        {
            // 未登录则转到登录页面
            return $this->_redirect(url('default/login'));
        }
    }

	/* ******************************************************************** */
    function actionTimecheck()
    {
        if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));
		
		//$show_box['title'] = '查询行为日志';
		//$this->_view['form'] = new Form_BeSearch(url('default::behaviorlog/list'));
		//查询，导出和批量删除
        //dump($_SESSION);

		$show_box['title'] = '查询详细行为日志';
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');
		$this->_view['form'] = new Form_BeHosts(url('default::systemlog/hostlist'));

		if(isset($this->_context->mac)){
			$this->_view['form']->add(QForm::ELEMENT, 'mac', array('_ui' => 'hidden','value' => $this->_context->mac));
		}

		if(isset($this->_context->id)){
			$this->_view['form']->add(QForm::ELEMENT, 'id', array('_ui' => 'hidden','value' => $this->_context->id));
		}
		//dump(Q::cache('rightitem'));
		if(Q::cache('rightitem')!=''){
			//重置下拉菜单
			$this->_view['form']->add(QForm::ELEMENT, 'groupid', array('_ui' => 'hidden','value' => Q::cache('rightitem')));
		}
    }

	function actionList(){
		
		$show_box['title'] = '系统日志首页';
		
		if ($this->_app->currentUserRoles())
        {
			Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');
			 //区分用户
			 $os = array("admin");

		     $aroles = array();
		     $aroles =split('[,]', $_SESSION['acl_task_userdata']['roles']);
			//dump($aroles);
			$isrightp=false;
			 foreach ($aroles as $value){
				if (in_array(strtolower($value), $os)) {
					$isrightp=true;
					//echo 'opip';
					break;
					//echo "Got Irix";
				}
			 }
			 //echo $isrightp;
			 //echo $_SESSION['acl_task_userdata']['user_id'];
			 $adming='';
			 if(!$isrightp){
				$havegroups = array();
				$select1 = Groupuser::find('userid = ?',$_SESSION['acl_task_userdata']['user_id'])->getAll();
				foreach ($select1 as $av){
					array_push($havegroups,$av->groupid);
					//echo $av->groupid;
				}
				$addconds='';
				//dump($havegroups);
				$hgs=count($havegroups);
				$this->_view['adming']=$hgs;
				if($hgs>0){
					$adming=' id in ('.join(",",$havegroups).') ';
				}else{
					$adming=' id in (-1) ';
				}
			 }else{
				
				$this->_view['adming']=-1;
			 }
			 //END

			$table =QDB::getConn();
			$sql = "SELECT DISTINCT groupid FROM `grouptohost`";
			$handel =$table->execute($sql);
			$temp = $handel->fetchAll();
			$allhave = array();
			foreach ($temp as $s){
				$allgroup = array();
				$allgroup['groupid']=$s['groupid'];
				$allhave[$s['groupid']]=$allgroup;
				//echo $s['groupid'];
			}

			//$table =QDB::getConn();
			$sql = "SELECT count( 1 ) AS grouphosts, groupid FROM `grouptohost` GROUP BY `grouptohost`.groupid";
			$handel =$table->execute($sql);
			$temp1 = $handel->fetchAll();
			//$allhosts = array();
			foreach ($temp1 as $s){
				//$allgroup = array();
				//$allgroup['groupid']=$s['groupid'];
				$allhave[$s['groupid']]['hosts']=$s['grouphosts'];
				//echo $s['groupid'];
			}

			//查询据现在20分钟内的主机
			$sql = "SELECT systeminfo.`macaddress`,hosttable.`id`,systeminfo.`lasttime` FROM systeminfo left join hosttable on hosttable.`macaddress` =systeminfo.`macaddress` WHERE systeminfo.`lasttime` >= DATE_sub( NOW( ) , INTERVAL 30 SECOND ) and systeminfo.`lasttime` < DATE_ADD( NOW( ) , INTERVAL 30 SECOND)";
			$handel =$table->execute($sql);
			$temp2 = $handel->fetchAll();
			$achosts = array();
			foreach ($temp2 as $s){
				if(isset($s['id']))
					array_push($achosts,$s['id']);
			}
			$allh=0;$alllivenum=0;
			if(count($achosts)>0){
				$allhaves=0;
				$allh=count($achosts);
				//统计每个组的激活数
				$sql = "SELECT count(1) as grouplives, groupid FROM `grouptohost` WHERE `hostid` IN (".join(",",$achosts).") GROUP BY groupid";
				$handel =$table->execute($sql);
				$temp3 = $handel->fetchAll();
				foreach ($temp3 as $s){
					$allhaves++;
					$allhave[$s['groupid']]['lives']=$s['grouplives'];
					$alllivenum=$s['grouplives']+$alllivenum;
				}
			}

			$sql = "SELECT count(`hosttable`.`id`) as allblanks  FROM `logdata`.`hosttable`  LEFT JOIN `grouptohost`   ON (grouptohost.hostid = hosttable.id)   WHERE ((grouptohost.groupid is null))";
			$hande9 =$table->execute($sql);
			$temp9 = $hande9->fetchAll();
			foreach ($temp9 as $s){
					$blankcounts=$s['allblanks'];
			}
			
			//print_r($blankcounts);
			$this->_view['blanklives']= $allh-$alllivenum;
			$this->_view['blankalls']= $blankcounts;
			$this->_view['groupc']= $allhave;

			//print_r($achosts);
			//$this->_view['groupc']= $allhave;
			// 设置页面列表数目
			$pagesize = 10;
			// 获取当前页数，通常为get方式参数
			$page = $this->_context->page;
			$this->_view['pageno']=$page;
			$select = Hostgroup::find($adming)->limitPage($page, $pagesize);
			$this->_view['pager'] = $select->getPagination();
			$this->_view['group'] = $select->order("id desc")->getAll();
			
			//$this->_view['form'] = new Form_BeSearch(url('default::systemlog/hostlist'));
			//$this->_view['form']->_subject = '查询系统日志';

			if($this->_view['pager']['record_count']>0 && $page=='')
					$page=1;

			$this->_view['countno'] = $this->_view['pager']['page_size']*$page-9;
			//echo $this->_view['pager']['record_count'];
			$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'systemlog|list|','\')',5);

			$this->_view['pagestr']= $pagediv->show();

		}else
        {
            // 未登录则转到登录页面
            return $this->_redirect(url('default/login'));
        }
	}

	/*Host Manage*/
	function actionHostlist()
    {
		if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));
		
		$show_box['title']='系统日志主机组';
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');
		//判断是否需要统计今天的数据
		$istoday=false;
		//判断是否跨越历史数据
		$iscross=false;
		$ishistory=false;
		$isfuntrue=false;
		$isfirst=true;
		$rightg='';
		//时间筛选
			$addconds=" time >= '".date("Y-m-d")." 00:00:00' and  time <= '".date("Y-m-d")." 23:59:59' ";
			//关键字筛选，IP筛选
			$addkeys='';$addips='';
			$this->_view['startd']='';
			$this->_view['endd']='';
			$allhave2=array();
			//$this->_view['form'] = new Form_BeSearch(url('default::behaviorlog/hostlist'));
			if ($this->_context->isPOST() && isset($_POST['startdate']) && isset($_POST['enddate']))
			{
				
				$allccs = new Helper_Counter();
				$nowtt=$allccs->AllCount($_POST['startdate'],$_POST['enddate']);
				
				$this->_view['startd']=$_POST['startdate'].'';
				$this->_view['endd']=$_POST['enddate'].'';
				$addconds="time >='".$_POST['startdate']."' and time<='".$_POST['enddate']."'";

				$allhave2=$allccs->Counter_config($nowtt,$addconds);
				//echo $this->my_date_add('d',$_POST['enddate'],1);
				//echo $addconds;
				//dump($addconds);
			}

			if(isset($_POST['groupid']))
				$rightg=''.$_POST['groupid'].'';

			$andstr='';
			if(isset($_POST['searchtxt']) && $_POST['searchtxt']!='')
					$addkeys=' hostname like \'%'.addslashes($_POST['searchtxt']).'%\'';

			//dump($addkeys);

				if(isset($_POST['searchip']) && $_POST['searchip']!=''){
					if($addkeys!='')$andstr=' and ';
					$addkeys.=$andstr.' ipaddr like \'%'.addslashes($_POST['searchip']).'%\'';
				}

			if($this->_context->startd !='' && $this->_context->endd !=''){
				$this->_view['startd']=$this->_context->startd;
				$this->_view['endd']=$this->_context->endd;
				$addconds="time >='".$this->_context->startd."' and time<='".$this->_context->endd."'";
				$enddstr=$this->my_date_add('d',date("Y-m-d"),1);
				
				$allccs = new Helper_Counter();
				$nowtt=$allccs->AllCount($this->_context->startd,$this->_context->endd);
				
				$this->_view['startd']=$this->_context->startd.'';
				$this->_view['endd']=$this->_context->endd.'';
				$addconds="time >='".$this->_context->startd."' and time<='".$this->_context->endd."'";

				$allhave2=$allccs->Counter_config($nowtt,$addconds);
			}else{
				if(!$this->_context->isPOST()){
					$allccs = new Helper_Counter();
					$addconds1="time >='".date("Y-m-d")." 00:00:00' and time<='".date("Y-m-d")." 23:59:59'";
					$allhave2=$allccs->Counter_config('1',$addconds1);
				}
			}

			
		//缓存主机组
		$table =QDB::getConn();
		$sql = "SELECT hostid, groupname FROM `group` JOIN `grouptohost` ON `grouptohost`.groupid = `group`.id";
		$handel =$table->execute($sql);
		$temp5 = $handel->fetchAll();
		$hgroups = array();
		foreach ($temp5 as $s){
			$hgroups[$s['hostid']]=array();
			array_push($hgroups[$s['hostid']],$s['groupname']);
			//echo $s['user_name'];
		}
		
		//dump($allhave2);
		$this->_view['hostc']= $allhave2;
		$this->_view['hostg']= $hgroups;

		$show_box['title'] = '主机列表';
		// 设置页面列表数目
		//$pagesize = 10;
		// 获取当前页数，通常为get方式参数
		//$page = $this->_context->page;
		//echo $page;
		//echo $this->_context->id;
		$allrit='';
		$this->_view['gid']=$this->_context->id;
		//$this->_view['pageno']=$page;
		if($this->_context->id!=0){
			// 查询指定用户 ID
			$edit_one = Hostgroup::find('id = ?', $this->_context->id )->query();
			if ($edit_one->groupname!='')
			{ // 如果用户 ID 无效，则返回用户管理首页
				$this->_view['gpname']=$edit_one->groupname;
			}
			
			$select = Host::find('[group.groupid]= ?',$this->_context->id);
			
		}
		else{
			//echo $addkeys;
			if($addkeys==''){
				if($rightg=='')
					$select = Host::find('grouptohost.groupid is null')->joinLeft('grouptohost', 'groupid', 'grouptohost.hostid = hosttable.id');
				else
					$select = Host::find('[group.groupid] in ('.$rightg.')');
			}else{
				if($rightg!='')
					$allrit=' and [group.groupid] in ('.$rightg.')';
				
				if(isset($_POST['searchip']) && $_POST['searchip']!='' && $_POST['searchtxt']=='')
					$select = Host::find('[group.groupid] is not null '.$allrit.' and ipaddr like ?','%'.$_POST['searchip'].'%');
				else if(isset($_POST['searchtxt']) && $_POST['searchtxt']!='' && $_POST['searchip']=='')
					$select = Host::find('[group.groupid] is not null '.$allrit.' and hostname like ?','%'.$_POST['searchtxt'].'%');
				else
					$select = Host::find('[group.groupid] is not null '.$allrit.' and ipaddr like ? and hostname like ?','%'.$_POST['searchip'].'%','%'.$_POST['searchtxt'].'%');
				
				//$select = Host::find($addkeys)->joinLeft('grouptohost', 'groupid', 'grouptohost.hostid = hosttable.id');
			}

			$this->_view['gpname']='';
		}
		//->limitPage($page, $pagesize);
		//$this->_view['pager'] = $select->getPagination();
		$this->_view['group'] = $select->order("id desc")->getAll();
		//$this->_view['countno'] = $this->_view['pager']['page_size']*$page+1;
		$this->_view['countno'] = 1;
    }

	function my_date_add($opt,$time,$num){
		$year = date('Y',strtotime($time));
		$month = date('m',strtotime($time));
		$day = date('d',strtotime($time));
		$last_day = date('t',strtotime($time));
		if($opt=='y'){
			$year = $year + $num;
		}else if($opt=='d'){
			$day = $day + $num;
		}else if($opt=='m'){
			$month = $month + $num;
			if($day == $last_day){//如果是最后一天的话
				$day = date('t',strtotime("{$year}-{$month}-01"));
			}
		} else {
			return $time;
		}
		return date("Y-m-d", mktime(0, 0, 0, $month, $day, $year));
	}

	function DateDiff($d1,$d2)
	{
		//日期比较函数
		 if(is_string($d1))$d1=strtotime($d1);
		 if(is_string($d2))$d2=strtotime($d2);
		 return ($d2-$d1)/86400; //这里返回的是天数，如果除以3600就是返回小时了，依此类推
	}

}